#!/bin/bash

set -e

K8_VERSION=1.21.14
DOCKER_VERSION=19.03.9
export DOCKER_DATA_ROOT="/data/docker"


# 步骤1：如果yum仓库文件不存在，则创建它
REPO_ALIYUN_FILE=/etc/yum.repos.d/CentOS-Base-aliyun.repo
REPO_EPEL_FILE=/etc/yum.repos.d/epel.repo
REPO_HUAWEI_FILE=/etc/yum.repos.d/CentOS-Base-huawei.repo
REPO_DOCKER_FILE=/etc/yum.repos.d/docker-ce.repo
REPO_K8S_FILE=/etc/yum.repos.d/kubernetes.repo

if [ ! -f "$REPO_ALIYUN_FILE" ]; then
    echo "下载 $REPO_ALIYUN_FILE 文件..."
    wget -O $REPO_ALIYUN_FILE https://mirrors.aliyun.com/repo/Centos-7.repo > /dev/null 2>&1
    echo "$REPO_ALIYUN_FILE 文件下载完成。"
else
    echo "仓库 $REPO_ALIYUN_FILE 已存在"
fi

if [ ! -f "$REPO_EPEL_FILE" ]; then
    echo "下载 $REPO_EPEL_FILE 文件..."
    wget -O $REPO_EPEL_FILE http://mirrors.aliyun.com/repo/epel-7.repo > /dev/null 2>&1
    echo "$REPO_EPEL_FILE 文件下载完成。"
else
    echo "仓库 $REPO_EPEL_FILE 已存在"
fi

if [ ! -f "$REPO_HUAWEI_FILE" ]; then
    echo "下载 $REPO_HUAWEI_FILE 文件..."
    wget -O $REPO_HUAWEI_FILE https://mirrors.huaweicloud.com/repository/conf/CentOS-7-anon.repo > /dev/null 2>&1
    echo "$REPO_HUAWEI_FILE 文件下载完成。"
else
    echo "仓库 $REPO_HUAWEI_FILE 已存在"
fi

if [ ! -f "$REPO_DOCKER_FILE" ]; then
    echo "下载 $REPO_DOCKER_FILE 文件..."
    wget -O $REPO_DOCKER_FILE http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo > /dev/null 2>&1
    echo "$REPO_DOCKER_FILE 文件下载完成。"
else
    echo "仓库 $REPO_DOCKER_FILE 已存在"
fi

if [ ! -f "$REPO_K8S_FILE" ]; then
    cat <<EOF > "$REPO_K8S_FILE"
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
    echo "已添加 Kubernetes 仓库。"
else
    echo "Kubernetes 仓库已存在。"
fi

# 步骤2：如果未安装所需的软件包，则进行安装
REQUIRED_PACKAGES=(ipset ipvsadm yum-utils device-mapper-persistent-data sshpass lvm2 docker-ce-$DOCKER_VERSION docker-compose docker-ce-cli-$DOCKER_VERSION kubectl-$K8_VERSION kubelet-$K8_VERSION kubeadm-$K8_VERSION bash-completion)
for package in "${REQUIRED_PACKAGES[@]}"; do
    if ! rpm -q "$package" > /dev/null 2>&1; then
        echo "正在安装 $package 请稍等... "
        yum install -y "$package" >/dev/null 2>&1
        echo "已安装 $package。"
    else
        echo "$package 已安装。"
    fi
done

# 步骤3：如果存在bash补全脚本，则进行源化
BASH_COMPLETION_SCRIPT=/etc/profile.d/bash_completion.sh
if [ -f "$BASH_COMPLETION_SCRIPT" ]; then
    source "$BASH_COMPLETION_SCRIPT"
    echo "已源化bash补全脚本。"
else
    echo "未找到bash补全脚本。"
fi

# 步骤4：如果 .bashrc 中未添加 kubectl 补全，则进行添加
BASHRC_FILE=~/.bashrc
if ! grep -q "source <(kubectl completion bash)" "$BASHRC_FILE"; then
    echo "source <(kubectl completion bash)" >> "$BASHRC_FILE"
    source "$BASHRC_FILE"
    echo "已将 kubectl 补全添加到 .bashrc。"
else
    echo "kubectl 补全已添加到 .bashrc。"
fi

# 步骤5：对 docker 进行初始化设置
echo "配置 Docker 镜像加速、数据目录"
mkdir -p /etc/docker/
cat > /etc/docker/daemon.json << EOF
{
  "registry-mirrors": [
    "https://docker.m.daocloud.io",
    "https://huecker.io",
    "https://dockerhub.timeweb.cloud",
    "https://noohub.ru"
  ],
  "exec-opts": ["native.cgroupdriver=systemd"],
  "data-root": "$DOCKER_DATA_ROOT"
}
EOF

echo " 启动 Docker 并设置开机自启动"
if ! systemctl is-enabled docker.service > /dev/null 2>&1; then
    systemctl enable docker.service --now > /dev/null
    echo "已启用并启动 docker.service。"
else
    echo "docker.service 已启用。"
fi


# 步骤6：如果 kubelet 服务未启用，则启用并启动它
echo " 启动 kubelet 并设置开机自启动"
if ! systemctl is-enabled kubelet.service > /dev/null 2>&1; then
    systemctl enable kubelet.service --now > /dev/null
    echo "已启用并启动 kubelet.service。"
else
    echo "kubelet.service 已启用。"
fi

